package com.yq.cp.controller.basics;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yq.cp.config.UserThreadLocal;
import com.yq.cp.entity.basics.InspectionItemManagement;
import com.yq.cp.entity.basics.InspectionPointManagement;
import com.yq.cp.entity.basics.PointInspectionItems;
import com.yq.cp.entity.common.ApiResult;
import com.yq.cp.entity.common.PageVo;
import com.yq.cp.service.basics.InspectionItemManagementService;
import com.yq.cp.service.basics.InspectionPointManagementService;
import com.yq.cp.service.basics.PointInspectionItemsService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 巡检点管理 前端控制器
 * </p>
 *
 * @author shenyuqing
 * @since 2024-08-27
 */
@RestController
@RequestMapping("/inspectionPointManagement")
public class InspectionPointManagementController {
    @Resource
    InspectionPointManagementService inspectionPointManagementService;
    @Resource
    PointInspectionItemsService pointInspectionItemsService;
    @Resource
    InspectionItemManagementService inspectionItemManagementService;
    /**
     * 添加编辑
     * @param inspectionPointManagement
     * @return
     */
    @PostMapping("add")
    public ApiResult add(@RequestBody InspectionPointManagement inspectionPointManagement){
        inspectionPointManagement.setCreateName(UserThreadLocal.get().getUsername());
        inspectionPointManagementService.saveOrUpdate(inspectionPointManagement);
        pointInspectionItemsService.remove(new QueryWrapper<PointInspectionItems>()
                .eq("patrol_point_id",inspectionPointManagement.getId()));
        if (inspectionPointManagement.getPointInspectionItems().size()>0) {
            for (PointInspectionItems item : inspectionPointManagement.getPointInspectionItems()) {
                if (Objects.isNull(item.getId()))
                    item.setPatrolPointId(inspectionPointManagement.getId());
                pointInspectionItemsService.saveOrUpdate(item);
            }
        }
        return ApiResult.success();
    }

    /**
     * 详情
     * @param inspectionPointManagement
     * @return
     */
    @PostMapping("detail")
    public ApiResult detail(@RequestBody InspectionPointManagement inspectionPointManagement){
        InspectionPointManagement byId = inspectionPointManagementService.getById(inspectionPointManagement);
        List<PointInspectionItems> items = pointInspectionItemsService.list(new QueryWrapper<PointInspectionItems>()
                .eq("patrol_point_id", inspectionPointManagement.getId()));
        ArrayList<InspectionItemManagement> arrayList = new ArrayList<>();
        if (!items.isEmpty()) {

            for (PointInspectionItems item : items) {
                InspectionItemManagement byId1 = inspectionItemManagementService.getById(item.getInspectionItemManagementId());
                arrayList.add(byId1);
            }
        }
        byId.setInspectionItemManagements(arrayList);
        //byId.setPointInspectionItems(items);
        return ApiResult.success(byId);
    }

    /**
     * 删除
     * @param inspectionPointManagement
     * @return
     */
    @PostMapping("remove")
    public ApiResult remove(@RequestBody InspectionPointManagement inspectionPointManagement){
        pointInspectionItemsService.remove(new QueryWrapper<PointInspectionItems>()
                .eq("inspection_item_management_id", inspectionPointManagement.getId()));
        inspectionPointManagementService.removeById(inspectionPointManagement);
        return ApiResult.success();
    }

    /**
     * 列表
     * @param vo
     * @return
     */
    @PostMapping("list")
    public ApiResult list(@RequestBody PageVo vo){
        Page<InspectionPointManagement> page = new Page<>(vo.getPage(), vo.getSize());
        QueryWrapper<InspectionPointManagement> wrapper = new QueryWrapper<>();
        if (!Objects.isNull(vo.getLike())&&!"".equals(vo.getLike()))
            wrapper.like("name",vo.getLike());
        wrapper.eq("site_id",vo.getSiteId());
        page = inspectionPointManagementService.page(page,wrapper);
        return ApiResult.success(page);
    }


}

